using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

/**
*@file       GWUser_EnterReciever.cs
*@brief      process message GWUser_Enter
*
*@author     graywind
*@version    1.0.0.1
*@data       2012/3/21
*/

//process GWUser_Enter
public class OnGWUser_Enter
{
    /// <summary>
    /// process message
    /// </summary>
    /// <param name="op">message operator</param>
    /// <param name="session_id">session identifier</param>
    /// <param name="msg_buffer">message buffer</param>
    /// <param name="msg_bytes">buffer length</param>
    public static void OnMapping(IMessageOperator op, Int32 session_id, byte[] msg_buffer, UInt32 msg_bytes)
    {
        //TODO
		gatewayserver.UsrEnter enter = ProtoNet<gatewayserver.UsrEnter>.CreateFromBytes(msg_buffer, msg_bytes);

        int error = -1;

        do
        {
			int gate_code;
            if (!op.UsrInfo.Add(enter.UsrId, enter.RoleCode, enter.UsrName, enter.WorldId, session_id, out gate_code))
            {
                error = (int)gatewayserver.EnterFailed.eErrorCode.Usr_Alreay_Online;
                break;
            }

            if(gate_code == GlobalDef.INVALID_GATE_CODE)
            {
                error = (int)gatewayserver.EnterFailed.eErrorCode.Max_Player_Arrived;
                break;
            }
			
			int server_session = op.GameServerGroupInfo.GetSessionId(enter.WorldId);
			if(server_session == NetLib.INVALID_SESSION)
			{
				error = (int)gatewayserver.EnterFailed.eErrorCode.Server_Not_Prepared;
				break;
			}
			
			msgLSPassport_Verify verfy_passport = new msgLSPassport_Verify(op.AuthHandle, enter.UsrId, enter.Passport, gate_code);
			gwMessagePort.ProcessMessage(verfy_passport);

        } while (false);

        if(error >= 0)
        {
            msgGWUser_Enter_Failed enter_failed = new msgGWUser_Enter_Failed(session_id, (gatewayserver.EnterFailed.eErrorCode)error);
            gwMessagePort.ProcessMessage(enter_failed);
        }
    }
}

//register callback
partial class MessageProcTypes<ProcType, MessageProc>
{
        [MessageProcType((int)GatewayServerTypes.MessageType.GWUser_Enter)]
        public static GatewayServerProcessor.GatewayServerTabFunc GWUser_EnterProc = new GatewayServerProcessor.GatewayServerTabFunc(OnGWUser_Enter.OnMapping);
}
